<?php
class article extends adminBase {
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("文章管理");
        $menuModule = MenuModule::init();
        // 生成菜单二维数组
        $menuTree = $menuModule->getMenuTree();
        $menuMap = array();
        foreach($menuTree as $subTree) {
            $menuMap[$subTree['name']] = array();
            foreach($subTree['children'] as $item) {
                $menuMap[$subTree['name']][$item['id']] = $item['name'];
            }
        }

        $this->fieldAdd("id", "ID", self::TYPE_PRIMARY, self::TABLE_STRING, self::FORM_HIDDEN);
        $this->fieldAdd("menu_id", "所属分类", self::TYPE_CUSTOM, self::TABLE_CUSTOM, self::FORM_SELECT, "base", self::FORM_SELECT)
            ->fieldModify("menu_id", array("map" => $menuMap))->fieldModify("menu_id", array("showFunction" => function ($data, $config) {
                $s = "";
                $ifFirst = true;
                foreach($data['menu'] as $menuItem) {
                    $s .= ($ifFirst? "": " / ") . $menuItem['name'];
                    if($ifFirst)
                        $ifFirst = false;
                }
                return $s;
            }));
        $this->fieldAdd("title", "标题", self::TYPE_VARCHAR, self::TABLE_STRING, self::FORM_TEXT, "base");
        $this->fieldAdd("pic_url","缩略图",self::TYPE_URL,self::TABLE_NONE,self::FORM_FILE, "base");
        $this->fieldAdd("owner","发布人",self::TYPE_FOREIGN,self::TABLE_MODE,self::FORM_NONE)
            ->fieldModify("owner",array(
                "mode"=>$this->user,
                "showFunction"=>function($data,$config){
                    return $data['user'];
                }
            ));
        $this->fieldAdd("publish_time", "发布时间", self::TYPE_TIMESTAMP, self::TABLE_DATE, self::FORM_DATE, "base");
        $this->fieldAdd("create_time", "创建时间", self::TYPE_TIMESTAMP, self::TABLE_DATETIME, self::FORM_NONE);
        $this->fieldAdd("read_times", "阅读次数", self::TYPE_VARCHAR, self::TABLE_STRING, self::FORM_NONE);
        $this->fieldAdd("praise", "点赞人数", self::TYPE_VARCHAR, self::TABLE_STRING, self::FORM_NONE);
        $this->fieldAdd("enable", "是否审核", self::TYPE_VARCHAR, self::TABLE_CUSTOM, self::FORM_NONE)->fieldModify("enable", array("showFunction" => function ($data, $config) {
                $now = $data['enable'] == 1? 1: 0;
                $text = $now == 1? "已通过": "未通过";
                $tips = $now == 1? "拒绝通过": "确认通过";
                $url = WebRouter::init()->getAction("changeEnable", "id={$data['id']}&enable=" . (1 - $now));
                return "$text(<a href='{$url}'>$tips</a>)";
            }));
        $this->fieldAdd("content", "内容", self::TYPE_TEXT, self::TABLE_NONE, self::FORM_UEDITOR, "content");

        $this->pageConfig("modify_group_name", array("base" => "基本信息", "content" => "文章内容"));

        // 以下是回执需要针对特定的文章而加上的，暂时不用
        //        $receiptUrl=WebRouter::init()->getPage("receipt","table","id=");
        //        $this->fieldAdd("receipt","回执信息",self::TYPE_CUSTOM,self::TABLE_CUSTOM)
        //            ->fieldModify("receipt",array("showFunction"=>function($data,$config)use($receiptUrl){
        //                return "<a href='{$receiptUrl}{$data['id']}'>查看回执信息</a>";
        //            }));

        $this->mode = ArticleMode::init();
        $this->addControl();
    }

    function tableTask($condition=array(), $sortArray=array(), $page=1){
        $sortArray+=array('create_time'=>"desc");
        $result=$this->getTableResult($condition, $sortArray, $page);
        $this->cms->codeTableScene($result);
    }

    function modifySubmitTask($data){
        $userInfo=$this->user->getLoginInfo();
        $up=Uploader::init();
        list($state,$info)=$up->upImage("pic_url",null,array("width"=>250,"height"=>null,"set"=>"thumb"));
        if(strtolower($state)=='success'){
            $data['pic_url']=$info['url'];
        }
        $data['owner']=$userInfo['id'];
        $dm=$this->mode->update($data);
        if($dm->judgeState()){
            $url=WebRouter::init()->getAction("table");
            View::displayAsTips($url,$dm->getTitle());
        }else{
            View::displayDataMessage($dm);
        }
    }

    function changeEnableTask($id,$enable){
        $articleMode=ArticleMode::init();
        $result['article']=$articleMode->getDetail($id);
        if(empty($result['article']))
            return new Intent(404,"不存在该文章");

        $data['id']=$id;
        $data['enable']=$enable;
        $dm=$articleMode->update($data);
        View::displayDataMessage($dm);
        return null;
    }
}